import 'package:flutter/material.dart';
import 'package:powerful_table/powerful_table.dart';

void main() {
  runApp(MyApp());
}

class Example extends StatelessWidget {
  Example({Key? key}) : super(key: key);

  final UnifyScrollController controller = UnifyScrollController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Example'),
      ),
      body: Container(
        height: 40,
        child: Row(
          children: [
            Expanded(
              child: SingleChildScrollView(
                controller: controller,
                child: Column(
                  children: [
                    Text('1'),
                    Text('1'),
                    Text('1'),
                    Text('1'),
                    Text('1'),
                    Text('1'),
                    Text('1'),
                    Text('1'),
                    Text('1'),
                    Text('1'),
                  ],
                ),
              ),
            ),
            Expanded(
              child: SingleChildScrollView(
                controller: controller,
                child: Column(
                  children: [
                    Text('2'),
                    Text('2'),
                    Text('2'),
                    Text('2'),
                    Text('2'),
                    Text('2'),
                    Text('2'),
                    Text('2'),
                    Text('2'),
                    Text('2'),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key? key}) : super(key: key);

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      // backgroundColor: Colors.red,
      appBar: AppBar(
        title: Text('Example'),
      ),
      body: Center(
        child: CustomScrollView(
          slivers: [
            SliverFreeTable(
              itemBuilder: (ctx, indexPath) {
                print(
                    "buld => item at row(${indexPath.row}) index(${indexPath.index})");
                if (indexPath.index == 0) {
                  return Align(
                    alignment: Alignment.centerLeft,
                    child:
                        Text("row(${indexPath.row})index(${indexPath.index})"),
                  );
                } else if (indexPath.index > 5) {
                  return Align(
                      alignment: Alignment.centerRight,
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                        children: [
                          Text("row(${indexPath.row})"),
                          Text("index(${indexPath.index})"),
                        ],
                      ));
                }
                return Align(
                  alignment: Alignment.centerRight,
                  child: Text("row(${indexPath.row})index(${indexPath.index})"),
                );
              },
              onTapCell: (ctx, index) {
                print("top => item at row($index)");
              },
              cellStyle: FreeTableCellStyle(
                  prefix: SizedBox(width: 15),
                  suffix: SizedBox(width: 15),
                  shadowOffset: 15,
                  itemWidthBuilder: (ctx, index) {
                    return 120;
                  },
                  heightBuilder: (ctx, index) {
                    return 60;
                  }),
              itemCount: 10,
              rowCount: 100,
            ),
          ],
        ),
      ),
    );
  }
}
